package com.winhxd.b2c.order.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.winhxd.b2c.common.constant.BusinessCode;
import com.winhxd.b2c.common.domain.PagedList;
import com.winhxd.b2c.common.domain.ResponseResult;
import com.winhxd.b2c.common.domain.order.condition.OrderUserManagementQueryCondition;
import com.winhxd.b2c.common.domain.order.vo.OrderInfoDetail4UserManagementVO;
import com.winhxd.b2c.common.exception.BusinessException;
import com.winhxd.b2c.common.feign.order.OrderUserManagementClient;
import com.winhxd.b2c.common.util.JsonUtil;
import com.winhxd.b2c.order.service.OrderUserManagementService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 用户管理中的订单信息
 * @author suhaochan
 * @date 2019/1/7 20:19:50
 */
@Api("OrderUserManagementController")
@RestController
public class OrderUserManagementController implements OrderUserManagementClient{

	private static final Logger LOGGER = LoggerFactory.getLogger(OrderUserManagementController.class);
	
	private static final int DEFAULT_PAGE_ZISE = 100;
	
	@Autowired
	private OrderUserManagementService orderUserManagementService;
	
	@Override
	@ApiOperation(value = "分销商订单列表",response = OrderInfoDetail4UserManagementVO.class, notes = "分销商订单列表")
	public ResponseResult<PagedList<OrderInfoDetail4UserManagementVO>> findDistributorOrder(
			@RequestBody OrderUserManagementQueryCondition condition) {
		String logTitle = "/api-order/order/4131/v1/findDistributorOrder/-分销商订单列表";
    	LOGGER.info("{}=--开始--{}", logTitle, JsonUtil.toJSONString(condition));
    	ResponseResult<PagedList<OrderInfoDetail4UserManagementVO>> result = new ResponseResult<>();
    	validateParamsCommon(condition);
    	PagedList<OrderInfoDetail4UserManagementVO> data = orderUserManagementService.findDistributorOrder(condition);
    	result.setData(data);
        LOGGER.info("{}=--结束 result={}", logTitle, result);
        return result;
	}

	@Override
	@ApiOperation(value = "分销商会员订单列表",response = OrderInfoDetail4UserManagementVO.class, notes = "分销商会员订单列表")
	public ResponseResult<PagedList<OrderInfoDetail4UserManagementVO>> findDistributorMemberOrder(
			@RequestBody OrderUserManagementQueryCondition condition) {
		String logTitle = "/api-order/order/4132/v1/findDistributorMemberOrder/-分销商会员订单列表";
    	LOGGER.info("{}=--开始--{}", logTitle, JsonUtil.toJSONString(condition));
    	ResponseResult<PagedList<OrderInfoDetail4UserManagementVO>> result = new ResponseResult<>();
    	validateParamsCommon(condition);
    	PagedList<OrderInfoDetail4UserManagementVO> data = orderUserManagementService.findDistributorMemberOrder(condition);
    	result.setData(data);
        LOGGER.info("{}=--结束 result={}", logTitle, result);
        return result;
	}

	@Override
	@ApiOperation(value = "查询会员订单列表",response = OrderInfoDetail4UserManagementVO.class, notes = "查询会员订单列表")
	public ResponseResult<PagedList<OrderInfoDetail4UserManagementVO>> findMemberOrder(
			@RequestBody OrderUserManagementQueryCondition condition) {
		String logTitle = "/api-order/order/4133/v1/findMemberOrder/-查询会员订单列表";
    	LOGGER.info("{}=--开始--{}", logTitle, JsonUtil.toJSONString(condition));
    	ResponseResult<PagedList<OrderInfoDetail4UserManagementVO>> result = new ResponseResult<>();
    	validateParamsCommon(condition);
    	PagedList<OrderInfoDetail4UserManagementVO> data = orderUserManagementService.findMemberOrder(condition);
    	result.setData(data);
        LOGGER.info("{}=--结束 result={}", logTitle, result);
        return result;
	}

	/**
	 * 
	 * @author suhaochan
	 * @date 2019年1月10日 下午3:27:43 
	 * @Description 校验公共参数
	 * @param 
	 * @return
	 */
	private void validateParamsCommon(OrderUserManagementQueryCondition condition) {
		if (condition == null || condition.getCustomerId()== null) {
    		throw new BusinessException(BusinessCode.CODE_403001);
    	}
		//默认从第1页开始查询，每页100条
    	if (condition.getPageNo() == 0) {
    		condition.setPageNo(1);
    	}
    	if (condition.getPageSize() == 0) {
    		condition.setPageSize(DEFAULT_PAGE_ZISE);
    	}
	}
}
